cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"
log using precinct_vote, replace
clear
*Open data with precinct id
use "dist p_id year update.dta", clear
keep p_id year llkk dist
keep if year==2010 |year==2006

*Add main data with indices
joinby p_id year using main_data, unmatched(none)
gen woman=Kon==2
keep  x y   galtan_pct  education woman stdoffshore_pct sector cap_exec_alt dist llkk year
drop if year <2006
drop if x==. |y==.
*Collapse data to precinct levenl
collapse  (mean) x y galtan_pct  education woman stdoffshore_pct sector cap_exec_alt , by( dist llkk year)
*Add election results
 joinby dist llkk year using "dist_election_02_14" , unmatched(none) 

 foreach var in x y {
	binscatter vsr_b `var', nq(100) scheme(s1mono) mcolor(dkorange) msymbol(smcircle) ///
		ylabel(, angle(horizontal) grid) ytitle(Vote Share Sweden Dem.)
	graph save    vsr_b_`var', replace
	binscatter vsr_s `var', nq(100) scheme(s1mono) mcolor(cranberry) msymbol(smcircle)  ///
		ylabel(, angle(horizontal) grid)   ytitle(Vote Share Social Dem.)
	graph save    vsr_s_`var' , replace
	binscatter vsr_g `var', nq(100) scheme(s1mono) mcolor(forest_green)  msymbol(smcircle)  ///
		ylabel(, angle(horizontal) grid)   ytitle(Vote Share Green Party)
	graph save    vsr_g_`var' , replace
	binscatter vsr_m `var', nq(100) scheme(s1mono) mcolor(navy)  msymbol(smcircle) ///
		ylabel(, angle(horizontal) grid)   ytitle(Vote Share Conservatives)
	graph save    vsr_m_`var' , replace
}
 
graph combine vsr_g_y.gph  vsr_g_x.gph  vsr_b_y.gph  vsr_b_x.gph ///
vsr_s_y.gph  vsr_s_x.gph  vsr_m_y.gph  vsr_m_x.gph, ///
col(2) xsize(18) ysize(20)  iscale(.6) scheme(s1mono)
graph save Figure_W4, replace

**Generate Figure W5
preserve

foreach party in s m g b {
    matrix C =J(7,1,.) 
    local s=0
	foreach var  in  galtan_pct  education woman   {
		local s=`s'+1
		corr `var'  vsr_`party'
		return list
		matrix C[`s',1]=r(rho)
	}
	local s=`s'+1
	foreach var  in  cap_exec  sector stdoffshore_pct {
		local s=`s'+1
		corr `var'  vsr_`party'
		return list
		matrix C[`s',1]=r(rho)
	}
	svmat C
	sum C*
	ren C1 est_`party'
}

gen pos_1 =_n-.1 if est_s!=.
gen pos_2= pos_1+.2

twoway (scatter est_b  pos_1 , msize(medlarge)  msymbol(circle)   mcolor(dkorange)) ///
	(scatter est_g  pos_2 , msize(medlarge)  msymbol(lgx)   mcolor(forest_green)) ///
	, ylabel(, angle(horizontal) grid) xline(4, lcolor(gs1) lpattern(dot))  ///
	yline(0, lcolor(gs1) lpattern(dash))  ytitle(Correlation Coefficient)  ///
	scale(1) ysize(15) xsize(20) scheme(s1mono) xscale(range(0.5 7.5))  ///
	xtitle(Party, size(medium))  legend(order(1 "Sweden Democrats" 2 "Green Party")) ///
	xlabel(	1 "Task Content" 2 "Education" 3  "Gender" 5 "Capital Ownership" 6 "Sector" 7 "Offshorability", labsize(medsmall) angle(horizontal))
graph save 	sd_green_votescorr, replace


twoway (scatter est_m  pos_1 , msize(medlarge)  msymbol(circle)   mcolor(navy)) ///
	(scatter est_s  pos_2 , msize(medlarge)  msymbol(lgx)   mcolor(cranberry)) ///
	, ylabel(,angle(horizontal) grid) xline(4, lcolor(gs1) lpattern(dot)) ///
	yline(0, lcolor(gs1) lpattern(dash))  ytitle(Correlation Coefficient)  ///
	scale(1) ysize(15) xsize(20) scheme(s1mono) xscale(range(0.5 7.5))  xtitle(Party, size(medium))  ///
	legend(order(1 "Conservatives" 2 "Social Democrats")) ///
	xlabel(	1 "Task Content" 2 "Education" 3  "Gender" 5 "Capital Ownership" 6 "Sector" 7 "Offshorability", labsize(medsmall) angle(horizontal))
graph save 	cons_soc_votescorr, replace

restore
graph combine sd_green_votescorr.gph  	cons_soc_votescorr.gph , col(2) iscale(1)	scheme(s1mono)	 ysize(8) xsize(20)	ycommon
graph save Figure_W5, replace
log close